﻿
Imports System.Runtime.CompilerServices
Imports osi.root.constants
Imports osi.root.connector
Imports osi.root.envs
Imports osi.root.utils

Public Module _environment_transform
    Sub New()
        assert(npos = -1)
    End Sub

    <Extension()> Public Function env_transform(ByVal i As String) As String
        If String.IsNullOrEmpty(i) Then
            Return i
        Else
            Const start_str As String = "$("
            Const end_str As String = ")"
            Dim s As Int32 = 0
            s = strindexof(i, start_str)
            While s <> npos
                Dim e As Int32 = 0
                e = strindexof(i, end_str, s + strlen(start_str), 1)
                If e <> npos Then
                    Dim r As String = Nothing
                    If env_value(strmid(i, s + strlen(start_str), e - s - strlen(start_str)),
                                 r) Then
                        strrplc(i, strmid(i, s, e - s + strlen(end_str)), r)
                        s += strlen(r)
                    Else
                        s += strlen(start_str)
                    End If
                    s = i.IndexOf(start_str, s)
                Else
                    Exit While
                End If
            End While

            Return i
        End If
    End Function
End Module
